/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-12 上午11:12
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.activities;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.adapter.consulation.ConsulationDataAdapter;
import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.bean.consulation.ConsulationData;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.utils.Dlog;
import com.example.dqhl.jdmetro.utils.JsonUtils;
import com.example.dqhl.jdmetro.utils.JumpItent;
import com.example.dqhl.jdmetro.utils.Toastutils;
import com.example.dqhl.jdmetro.utils.recycleview.ConsulationRecycleViewLayoutManager;
import com.example.dqhl.jdmetro.utils.recycleview.ConsulationRecycleviewDecoration;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SearchActivity extends BaseActivity {

    private String searchText = "";

    //资讯列表
    @Bind(R.id.rv_consulationList)
    RecyclerView rv_consulationList;
    private List<ConsulationData.DataBean> consulationDataList = new ArrayList<>();
    private List<ConsulationData.DataBean.PicAllBean> pic_allList = new ArrayList<>();
    private ConsulationDataAdapter consulationDataAdapter;

    @Override
    public void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_search);
        initTitleBar(R.string.txt_search);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        searchText = getIntent().getStringExtra("searchText");
        //资讯列表
        ConsulationRecycleViewLayoutManager linearLayoutManager_list = new ConsulationRecycleViewLayoutManager(this);
        linearLayoutManager_list.setOrientation(LinearLayoutManager.VERTICAL);
        rv_consulationList.setLayoutManager(linearLayoutManager_list);
        rv_consulationList.setAdapter(consulationDataAdapter = new ConsulationDataAdapter(this));
        doSearch();
        rv_consulationList.addItemDecoration(new ConsulationRecycleviewDecoration(this, ConsulationRecycleviewDecoration.VERTICAL_LIST));

    }


    @Override
    public void initListener() {

    }

    /**
     * 搜索
     */
    private void doSearch() {
        RequestParams params = new RequestParams(Config.consulation_search);
        params.addBodyParameter("content", searchText);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getData(result);
                if (errCode == 200) {
                    ConsulationData consulationData = new Gson().fromJson(result, ConsulationData.class);
                    consulationDataList = consulationData.getData();
                    consulationDataAdapter.refresh_list(consulationDataList);
                    consulationDataAdapter.setOnItemClickListener(new ConsulationDataAdapter.onItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            if(user != null){
                                Bundle bundle = new Bundle();
                                bundle.putString("info_id", consulationDataList.get(position).getInfo_id());
                                JumpItent.jump(SearchActivity.this, ConsulationDeatilActivity.class, bundle);
                            }else{
                                Toastutils.show("请先登录");
                            }

                        }
                    });
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
